package com.tarena.lbs.basic.service.impl;

import com.tarena.lbs.basic.dao.repository.TagRepository;
import com.tarena.lbs.basic.pojo.po.TagLibraryPO;
import com.tarena.lbs.basic.pojo.vo.TagLibraryVO;
import com.tarena.lbs.basic.pojo.vo.TagVO;
import com.tarena.lbs.basic.service.TagService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagRepository tagRepository;

    @Override
    public Map<String, List<TagVO>> getTagsByType(int tagType) {
        // 1. 从数据层查询指定类型的所有标签
        List<TagLibraryPO> pos = tagRepository.getTagsByType(tagType);
        log.info("查询的类型:{}, 查询到的标签有:{}", tagType, pos);
        
        // 2. 封装转化为Map结构
        Map<String, List<TagVO>> mapVo = assembleTags2Map(pos);
        log.info("封装最终的结果map:{}", mapVo);
        return mapVo;
    }

    @Override
    public List<TagVO> getAllEnabledTags() {
        List<TagLibraryPO> pos = tagRepository.getAllEnabledTags();
        return pos.stream().map(this::poToTagVO).collect(Collectors.toList());
    }

    @Override
    public List<TagVO> getTagsByParentId(Integer parentId) {
        List<TagLibraryPO> pos = tagRepository.getTagsByParentId(parentId);
        return pos.stream().map(this::poToTagVO).collect(Collectors.toList());
    }

    private Map<String, List<TagVO>> assembleTags2Map(List<TagLibraryPO> pos) {
        Map<String, List<TagVO>> mapVo = new HashMap<>();
        
        // 1. 筛选出一级标签（parentId为0的标签）
        List<TagLibraryPO> firstLevelPos = pos.stream()
            .filter(po -> po.getTagParentId().equals(0))
            .collect(Collectors.toList());
        
        // 2. 对一级标签进行循环处理
        firstLevelPos.forEach(firstPo -> {
            String firstTagName = firstPo.getTagName(); // map的key值：美食、旅游
            Integer firstTagId = firstPo.getId(); // 一级标签ID，作为二级标签的parentId
            
            // 3. 筛选出属于当前一级标签的二级标签
            List<TagLibraryPO> secondLevelPos = pos.stream()
                .filter(po -> po.getTagParentId().equals(firstTagId))
                .collect(Collectors.toList());
            
            // 4. 将二级标签PO转换为TagVO格式
            List<TagVO> value = secondLevelPos.stream().map(po -> {
                TagVO vo = new TagVO();
                vo.setTagCategoryName(firstTagName); // 设置一级标签名称
                TagLibraryVO voTag = new TagLibraryVO();
                BeanUtils.copyProperties(po, voTag);
                vo.setTagLibraryBO(voTag);
                return vo;
            }).collect(Collectors.toList());
            
            mapVo.put(firstTagName, value);
        });
        
        return mapVo;
    }

    private TagVO poToTagVO(TagLibraryPO po) {
        TagVO vo = new TagVO();
        // 获取父标签名称作为分类名称
        if (po.getTagParentId() != 0) {
            TagLibraryPO parentPo = tagRepository.getTagById(po.getTagParentId());
            if (parentPo != null) {
                vo.setTagCategoryName(parentPo.getTagName());
            }
        }
        TagLibraryVO voTag = new TagLibraryVO();
        BeanUtils.copyProperties(po, voTag);
        vo.setTagLibraryBO(voTag);
        return vo;
    }
}